<template>
  <div class="home">

    <swiper :list="carsouel" dots-position="center" :loop="true" :auto="true" :show-desc-mask="false"></swiper>

    <div class="show-block">
      <button class="more-btn" @click="toSample">更多样板房</button>
      <SampleCp v-for="sample of sampleList" :key="sample.id" :sample="sample"></SampleCp>
    </div>

    <div class="show-block" style="margin-top: 10px">
      <button class="more-btn more-pro-btn" @click="toProduct">更多套餐</button>
      <flexbox v-for="(productList, index) of productFmt" :key="index">
        <flexbox-item v-for="product of productList" :key="product.id">
          <ProductCp :product="product" :class="product.id % 2 !== 0 ? 'left' : 'right'"></ProductCp>
        </flexbox-item>
      </flexbox>
    </div>
    
  </div>
</template>

<script>
import { Swiper, Flexbox, FlexboxItem } from 'vux'
import SampleCp from './public/sampleCp'
import ProductCp from './public/productCp'

export default {
  components: {
    Swiper,
    Flexbox,
    FlexboxItem,
    SampleCp,
    ProductCp
  },
  created () {
    this.getAllCarousel()
    this.getSamples()
    this.getProduct()
  },
  data () {
    return {
      carsouel: [],
      sampleList: [],
      productFmt: []
    }
  },
  methods: {
    getAllCarousel () {
      this.$http.get('/carousel/list').then(res => {
        if (res.body.response) {
          this.carsouel = res.body.response
        }
      })
    },
    getSamples () {
      this.$http.get('/sample/getAllSample').then(res => {
        if (res.body.response) {
          this.sampleList = res.body.response
        }
      })
    },
    getProduct (isRefresh) {
      let params = {
        pageNum: 1,
        pageSize: 4
      }
      this.$http.post('/product/list', params).then(res => {
        if (res.body.response) {
          let productList = res.body.response.list
          for (let i = 0; i < productList.length; i += 2) {
            this.productFmt.push(productList.slice(i, i + 2))
          }
        }
      })
    },
    toSample () {
      this.$router.push({name: 'sample'})
    },
    toProduct () {
      this.$router.push({name: 'product'})
    }
  }
}
</script>

<style lang="less">
.home {
  padding-bottom: 60px;
  .show-block {
    position: relative;
    margin-top: 20px;
  }
  .more-btn {
    position: absolute;
    z-index: 9;
    top: -8px;
    right: 2vw;
    height: 30px;
    width: 90px;
    border: none;
    background: @theme-color;
    color: #fff;
    border-radius: 5px;
    box-shadow: 0 5px 15px @theme-shadow-color;
  }
  .more-pro-btn {
    position: absolute;
    top: 4px;
  }
  .left {
    margin-left: 4vw;
  }
  .right {
    margin-right: 4vw;
  }
}
</style>
